<?php
//ob_start();
include_once('settings.include.php');
include_once('includes/appcfg/connectBD.php');

 
function ValidaUtilizador(){
    global $controller ;
    global $conn ;
    
    $usr = & $controller->getUser();
    
    $params['username']=
    
    
    $params['username']=$_GET['txtUtilizador'];   
    $params['password']=$_GET['txtPassword'];
    
    $credential = new SHA1BasicCredential();
    $validatorManager = new ValidatorManager() ;
    $validatorManager->register('username',new StringValidator());
    $validatorManager->register('password',new StringValidator());
    $validatorManager->execute($controller,$usr,$params);
    $validationErrors=$validatorManager->getExecutionErrors();
    
    if (!$validationErrors){
        $credential->setUsername($params['username']);
        $credential->setPassword($params['password']);
        $usr->setCredential($credential);
        $authH = new FAPDBSQLAuthenticationHandler(array());  
        $authH->setConnection($conn); 
            if ($authH->login($controller,$usr)){
                $usr->setAuthenticated(true);
                return true;
            }
        return false;
    }else{
        print_r($validationErrors);
    }
    
}

 
function ValidaUtilizadorLDAP(){
    global $controller ;
    global $conn ;
    
    $usr = & $controller->getUser();

    
    $params['username']=$_GET['txtUtilizador'];   
    $params['password']=$_GET['txtPassword'];
    
    $ldapconfig['server'] = '135.177.22.91';
    $ldapconfig['port'] = 389;
    $ldapconfig['basedn'] = 'ou=PORTAL-USERS, ou=Testes, ou=Aplications, ou=ZENWorks, ou=RIALFA, o=FAP';    
    $credential = new BasicCredential();
    
    $validatorManager = new ValidatorManager() ;
    $validatorManager->register('username',new UsernameValidator());
    $validatorManager->register('password',new PasswordValidator());
    
    $validatorManager->execute($controller,$usr,$params);
    $validationErrors=$validatorManager->getExecutionErrors();
    
    if (!$validationErrors){
        $credential->setUsername($params['username']);
        $credential->setPassword($params['password']);
        $usr->setCredential($credential);
        $authH = new LDAPAuthenticationHandler($ldapconfig);  
        if ($authH->login($controller,$usr)){
            $usr->setAuthenticated(true);
            return true;
        }
        return false;
    }else{
        print_r($validationErrors);
    }
    
}
 

//header('WWW-Authenticate: Basic realm="'.$ldapconfig['authrealm'].'"');
$PHP_AUTH_USER="POSilva";
$PHP_AUTH_PWD="canms2";
//LDAPTest( $PHP_AUTH_USER,$PHP_AUTH_PWD );
/*
if (ValidaUtilizador()) {
//     header("Location: ".$controller->buildURL("sucesso.php"));
}else{
//     header("Location: ".$controller->buildURL("erro.php"));
}
*/

if (ValidaUtilizadorLDAP()) {
    $usr = & $controller->getUser();
    $fn =$usr->getAttribute('fullname');
    print($fn[0]);
    session_unset();
}else{
    echo "NOK";
//     header("Location: ".$controller->buildURL("erro.php"));
}










//ob_flush();
?>